﻿using System;
using System.Collections;
using System.Configuration;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

namespace DrawMap
{
    public partial class Sample : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            // set up the map control. This must be done before the test for
            // post back
            SetupMap();

            if (IsPostBack)
                return;

            // handle when there's a click on the image - a click on the image
            // is implemented as a link to some (this) page with key information
            // in the HTTP Request.
            if (null != Request["layer"])
            {
                ViewMapControl.Image_Click();
                country.Text = Request["key"];
            }
        }

        // Do set up of the map viewer control
        protected void SetupMap()
        {
            //-----------------------------------------------------------------
            // Step 4 - set up the map viewer control properties
            //-----------------------------------------------------------------

            // the pixel size of the actual image
            ViewMapControl.DisplaySize = new Size(1500, 1500);

            // the pixel size of the viewport (image display)
            ViewMapControl.ViewPortSize = new Size(500, 500);

            // the callback url for the click-map
            ViewMapControl.MapUrl = "Sample.aspx";

            // display units in scale text 
            ViewMapControl.MapUnits = "Units";

            // how far should the envelope boundary be expanded around a selected
            // polygon
            ViewMapControl.EnvelopeBoundary = 2F;

            // should the control generate a clickable map of the resulting image?
            ViewMapControl.GenerateClickMap = true;

            // create our layers and describe how to draw them
            LoadMap();

            // set default selection layer - this is also the layer searched when
            // selecting a polygon by key value.
            ViewMapControl.SelectionLayerName = "country";
        }

        // Identify the layer to the map viewer control. Each layer represents
        // data from a table containing a SqlGeometry column to hold the shapes
        // and various attribute columns to hold related data.
        protected void LoadMap()
        {
            //-----------------------------------------------------------------
            // Step 5 - Create layers, one for each geometry to draw
            //-----------------------------------------------------------------
            
            // create a layer that gets data from a sql data source
            MapViewer.SqlDataSourceLayer country = new MapViewer.SqlDataSourceLayer();
            
            // Set the sql data source - must point to a SQL2008 database, but it 
            // can be the Express version
            country.source = SqlDataSource;
            
            // define the table that contains the geometries for this layer
            country.tablename = "americas_adm0";
            
            // define the SqlGeometry column that contains our lines or polygons
            country.tablegeocolumn = "geom";

            // define the column that uniquely identifies a record
            country.keycolumn = "Country";

            // define layer drawing attributes
            country.LayerDrawingOptions.LinePen = new Pen(Brushes.Gray, 0.5F);
            country.LayerDrawingOptions.SelectedLinePen = new Pen(Brushes.Red, 1);

            // do we display onscreen text when the mouse rolls over a polygon?
            country.displaykey = true;

            // sidebar display values
            country.AddDisplayColumn("Country", "Country");

            // add this layer to the map viewer control
            ViewMapControl.Add("country", country);
        }

        // Handle the button click - do search by user provided data element
        protected void doit_Click(object sender, EventArgs e)
        {
            if (!String.IsNullOrEmpty(country.Text))
            {
                // identify the layer that contains this data element
                ViewMapControl.SelectionLayerName = "country";

                // store the column name and data to search for
                ViewMapControl.SelectionValues.Add(new Parameter("Country", DbType.String, country.Text));

                // go draw our map - this will select the polygon(s) for the indicated
                // data element, then expand their envelope by the "EnvelopeBoundary" 
                // value and draw the shapes in the selected region.
                ViewMapControl.DrawMapByKey();
            }
        }

    }
}
